home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / netlog-1.02 / extract / lex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-20  |  3.1 KB  |  171 lines

  1. /*
  2.      extract - A network log processor
  3.      Copyright (C) 1993 Douglas Lee Schales, David K. Hess, David R. Safford
  4.  
  5.      Please see the file `COPYING' for the complete copyright notice.
  6.  
  7. lex.c - 03/20/93
  8.  
  9. */
  10. #include <stdio.h>
  11. #include <ctype.h>
  12. #include <malloc.h>
  13. #include "y.tab.h"
  14. #include "lex.h"
  15. #include "chario.h"
  16.  
  17. #define TRUE 1
  18. #define FALSE 0
  19.  
  20. extern int atoi(char *);
  21.  
  22. static int buflen = 0;
  23. static int bufptr = 0;
  24. static char *buffer = (char *)0;
  25.  
  26. extern struct keywords keywords[];
  27.  
  28. int     
  29. yylex()
  30. {
  31.      int c, nc;
  32.      int comment = FALSE;
  33.      int number;
  34.      int i;
  35.      int quote;
  36.      
  37.      while((c = fetchar()) != EOF){
  38.       if(c == '#')
  39.            comment = TRUE;
  40.       else if(comment){
  41.            if(c == '\n')
  42.             comment = FALSE;
  43.       }
  44.       else if(!isspace(c))
  45.            break;
  46.      }
  47.      if(c == EOF)
  48.       return 0;
  49.      
  50.      buflen = 16;
  51.      buffer = (char *)malloc(buflen);
  52.  
  53.      quote = FALSE;
  54.      if(isalnum(c) || c == '"'){
  55.       bufptr = 0;
  56.       if(c == '"'){
  57.            quote = TRUE;
  58.            number = FALSE;
  59.       }
  60.       else {
  61.            buffer[bufptr++] = c;
  62.            number = isdigit(c);
  63.       }
  64.       while((c=fetchar()) != EOF){
  65.            if(c == '"'){
  66.             if(quote)
  67.              c = fetchar();
  68.             break;
  69.            }
  70.            else if(!quote){
  71.             if(isspace(c))
  72.              break;
  73.             if(c == '<' ||
  74.                c == '>' ||
  75.                c == '&' ||
  76.                c == '|' ||
  77.                c == '!' ||
  78.                c == '{' ||
  79.                c == '}' ||
  80.                c == ';' ||
  81.                c == '(' ||
  82.                c == ')' ||
  83.                c == ':' ||
  84.                c == '/' ||
  85.                c == '=')
  86.              break;
  87.             if(bufptr && number && !isdigit(c) && !isalpha(c))
  88.              break;
  89.             if(!isdigit(c))
  90.              number = FALSE;
  91.            }
  92.            if(bufptr == buflen){
  93.             buflen *= 2;
  94.             buffer = (char *)realloc(buffer, buflen);
  95.            }
  96.            buffer[bufptr++] = c;
  97.       }
  98.       pushchar(c);
  99.       if(bufptr == buflen){
  100.            buflen *= 2;
  101.            buffer = (char *)realloc(buffer, buflen);
  102.       }
  103.       buffer[bufptr] = 0;
  104.       if(number){
  105.            int value = atoi(buffer);
  106.            free(buffer);
  107.            yylval.intval = value;
  108.            if(value < 256)
  109.             return BYTEVAL;
  110.            return INTEGER;
  111.       }
  112.  
  113.       if(!quote)
  114.            for(i=0;keywords[i].name;i++)
  115.             if(!strcmp(buffer, keywords[i].name)){
  116.              free(buffer);
  117.              yylval.intval = keywords[i].token;
  118.              return keywords[i].token;
  119.             }
  120.       yylval.strval = buffer;
  121.       return STRING;
  122.      }
  123.  
  124.      switch(c){
  125.      case '<':
  126.       nc = fetchar();
  127.       if(nc == '='){
  128.            yylval.intval = LEQ;
  129.            return LEQ;
  130.       }
  131.       pushchar(nc);
  132.       break;
  133.      case '>':
  134.       nc = fetchar();
  135.       if(nc == '='){
  136.            yylval.intval = GEQ;
  137.            return GEQ;
  138.       }
  139.       pushchar(nc);
  140.       break;
  141.      case '!':
  142.       nc = fetchar();
  143.       if(nc == '='){
  144.            yylval.intval = NEQ;
  145.            return NEQ;
  146.       }
  147.       pushchar(nc);
  148.       break;
  149.      case '&':
  150.       nc = fetchar();
  151.       if(nc == '&'){
  152.            yylval.intval = AND;
  153.            return AND;
  154.       }
  155.       pushchar(nc);
  156.       break;
  157.      case '|':
  158.       nc = fetchar();
  159.       if(nc == '|'){
  160.            yylval.intval = OR;
  161.            return OR;
  162.       }
  163.       pushchar(nc);
  164.       break;
  165.      default:
  166.       break;
  167.      }
  168.      yylval.intval = c;
  169.      return c;
  170. }
  171.